package resources;

import java.io.File;

/**
 * utility class which is a place holder for all common resources and functions to be accessed
 *
 */
public class ResourceProvider 
{

    public static final String APPLICATION_NAME = "Email Application";
    
    public static final String ADD_FOLDER = "Add New Folder";
    
    public static final String DELETE_FOLDER = "Delete";
    
    public static final String OPEN = "OPEN";
	
	public static final String MAILBOX = "Mailbox";
	
	public static final String INBOX = "Inbox";
	
	public static final String OUTBOX = "Outbox";
	
	public static final String SENT = "Sent";
	
	public static final String TRASH = "Trash";
	
	public static final String DRAFT = "Draft";
	
	public static final String TEMPLATES = "Templates";
	
	public static final String FILTERS = "Filters";
	
	public static final String MOVE_TO = "Move to";
	
	public static final String NEW_MESSAGE = "New Message";
	
	public static final String NEW_MEETING = "New Meeting";
	
	public static final String LOG_OUT = "Log Out";
	
    public static final String DELETE_OPTION = "Delete";
    
    public static final String SEND_OPTION = "Send";
    
    public static final String CANCEL_OPTION = "Cancel";
    
    public static final String SAVE_TO_DRAFT_OPTION = "Save To Draft";
    
    public static final String REPLY_OPTION = "Reply";
        
    public static final String CREATETEM_OPTION = "Create Template";
    
    public static final String EDITTEM_OPTION = "Edit Template";
    
    public static final String SAVE_OPTION = "Save";
    
    public static final String LOAD_OPTION = "Load Template";
	
	public static final String PERSISTENCE_USERS_FOLDER_PATH = "persistence" + getFilePathSeperator() + "users" + getFilePathSeperator();
	
	// In milliseconds
	public static final int EMAIL_DELAY = 100;

    public static final String SEARCH = "Search";
    
    public static final String SEARCH_REGEX = "RegEx Search";

    public static final String EDIT_TEMPLATE = "Edit/Delete Template";
    
    public static final boolean SIMULATTION = false;

    public static final String CREATE_FILTER_OPTION = "Create Filter";

    public static final String FILTER_SETTING = "Filter Setting";
    
    public static final int SEARCH_EMAIL_FILENAME = 1;
    
    public static final int SEARCH_EMAIL_MESSAGE = 2;
    
    public static final int SEARCH_EMAIL_SUBJECT = 3;
    
    public static final int SEARCH_EMAIL_RECIPIENTS = 4;
    
    public static final int SEARCH_EMAIL_FROM = 5;

    public static final String ACCEPT = "Accept";
    
    public static final String REJECT = "Reject";
    
    public static final String EDIT = "Edit";

    public static final String REQUEST_UPDATE = "Request to update";

    public static final String UPDATED = "UPDATED";

    public static final String MEETING = "Meeting";

    public static final String MY_MEETING = "Meeting Board";

    public static final String NO_ACTION = "No Action";

    public static final String MEETING_DELETED = "Meeting Deleted";

    public static final int NETWORK_WAITING_PERIOD = 2000;

    public static final String SERVER = "SERVER";
    
    public static final String SERVER_FOLDER = "SERVER";
    
    public static final String SERVER_MEETING_FOLDER = "Meetings";

    public static final int MAIN_SERVER_PORT = 9111;

    public static final int CLIENT_PORT = 9110;

    public static final String UPDATE_MEETING = "Update Meeting";
    
    public static final String DELETE_MEETING = "Delete Meeting";
    
    /**
     * check if it is MAC OS
     * @return return true if MC OS else false
     */
    public static boolean isMac()
	{
		String OS = System.getProperty("os.name").toLowerCase();

		if (OS.indexOf("mac") >= 0) 
		{
			return true;
		}
		else if((OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 ))
			return true;
		
		return false;
	}
	
	/**
	 * get give root folder
	 * @param path
	 * @return the root folder name
	 */
	public static String getRootFolder(String path)
	{
	    String seperator = ResourceProvider.getFilePathSeperator();
	    
        String mailboxFolderPath = ResourceProvider.MAILBOX + seperator;
        
        if(path.contains(mailboxFolderPath + ResourceProvider.INBOX))
        {
            return ResourceProvider.INBOX;
        }
        else if(path.contains(mailboxFolderPath + ResourceProvider.DRAFT))
        {
            return ResourceProvider.DRAFT;
        }
        else if(path.contains(mailboxFolderPath + ResourceProvider.OUTBOX))
        {
            return ResourceProvider.OUTBOX;
        }
        else if(path.contains(mailboxFolderPath + ResourceProvider.SENT))
        {
            return ResourceProvider.SENT;
        }
        else if(path.contains(mailboxFolderPath + ResourceProvider.TRASH))
        {
            return ResourceProvider.TRASH;
        }
        else if(path.contains(mailboxFolderPath + ResourceProvider.TEMPLATES))
        {
            return ResourceProvider.TEMPLATES;
        }
        else
        {
            // send Inbox
            return ResourceProvider.INBOX;
        }
	}

	/**
	 * get root folder path
	 * @param actionFolder
	 * @param user
	 * @return the root folder path
	 */
    public static String getRootFolderPath(String actionFolder, String user)
    {
        String seperator = ResourceProvider.getFilePathSeperator();
        String userFolderPath = ResourceProvider.PERSISTENCE_USERS_FOLDER_PATH + user;
        String mailboxFolderPath = userFolderPath + seperator + ResourceProvider.MAILBOX;
        
        if(ResourceProvider.INBOX.equals(actionFolder))
        {
            String inboxFolderPath = mailboxFolderPath + seperator + ResourceProvider.INBOX;
            return inboxFolderPath;
        }
        else if(ResourceProvider.OUTBOX.equals(actionFolder))
        {
            String outboxFolderPath = mailboxFolderPath + seperator + ResourceProvider.OUTBOX;
            return outboxFolderPath;
        }
        else if(ResourceProvider.SENT.equals(actionFolder))
        {
            String sentFolderPath = mailboxFolderPath + seperator + ResourceProvider.SENT;
            return sentFolderPath;
        }
        else if(ResourceProvider.DRAFT.equals(actionFolder))
        {
            
            String draftFolderPath = mailboxFolderPath + seperator + ResourceProvider.DRAFT;
            return draftFolderPath;
        }
        else if(ResourceProvider.TRASH.equals(actionFolder))
        {   
            String thrashFolderPath = mailboxFolderPath + seperator + ResourceProvider.TRASH;
            return thrashFolderPath;
        }
        else if(ResourceProvider.TEMPLATES.equals(actionFolder))
        {   
            String templateFolderPath = mailboxFolderPath + seperator + ResourceProvider.TEMPLATES;
            return templateFolderPath;
        }
        else if(ResourceProvider.FILTERS.equals(actionFolder))
        {   
            String fiterFolderPath = mailboxFolderPath + seperator + ResourceProvider.FILTERS;
            return fiterFolderPath;
        }
        else if(ResourceProvider.MAILBOX.equals(actionFolder))
        {   
            return mailboxFolderPath;
        }
        else if(ResourceProvider.MEETING.equals(actionFolder))
        {   
            String meetingFolderPath = mailboxFolderPath + seperator + ResourceProvider.MEETING;
            return meetingFolderPath;
        }
        else if(ResourceProvider.SERVER_FOLDER.equals(actionFolder))
        {   
            String serverFolderPath = userFolderPath + seperator + ResourceProvider.SERVER;
            return serverFolderPath;
        }
        else if(ResourceProvider.SERVER_MEETING_FOLDER.equals(actionFolder))
        {   
            String serverFolderPath = userFolderPath + seperator + ResourceProvider.SERVER;
            String serverMeetingFolderPath = serverFolderPath + seperator + ResourceProvider.SERVER_MEETING_FOLDER;
            return serverMeetingFolderPath;
        }
        else
        	return "";
    }
    
    /**
     * convert date format to persistence format (replace / with _)
     * @param originalFormat
     * @return the converted data format for the persistence
     */
    public static String converDataFormatToPersistenceFormat(String originalFormat)
    {
        String newFormat = originalFormat.replace("/", "_").replace(":", "_").replace(" ", "_");
        return newFormat;
    }
    
    /**
     * get file path separator based on OS being
     * @return the file path based on the OS
     * 
     */
    public static String getFilePathSeperator()
    {
        return File.separator;
    }
    
    /**
     * get email file name form the given path
     * @param filePath
     * @return the file name of the path
     */
    public static String getFileNameFromPath(String filePath)
    {
        String seperator = "";
        
        if(ResourceProvider.isMac())
        {
            seperator = "/";
        }
        else
        {
            seperator = "\\\\"; 
        }
        
        String[] spl = filePath.split(seperator);
        String fileName = spl[(spl.length-1)];
        
        return fileName;
    }
	
}